// pages/goucai/goucai.js
var utils = require('../../utils/api.js');
var base64 = require('../../utils/base64.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    num: 1, //当前购买彩种数量
    type: 1, //类型
    // imageUrl: '../../assets/02.png', //彩种图片
    imageUrl: '../../assets/nofoundlotterypic.png', //彩种图片
    imageUrltrue: '', //彩种图片   
    imageHidden: true,
    standbyTicket: 0, //当前机头余量
    price: 0, //当前彩票单价
    totalPrice: 0, //当前订单总价
    buyUpperLimit: 0, //当前最高可购买的彩票
    buyNavigateTo: '/pages/dingdanxiangqing/dingdanxiangqing', //跳转地址
    lotteryName: '', //彩种名称
    urlUnicode: '', //url地址
    disabled: false, //连点控制器
    isHiddenbuyBtn: false, //提交订单按钮是否显示
    allLoading: false, //加载判断
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('onLoad')
    // console.log(options)
    var that = this
    if (options.q) {
      that.setData({
        urlUnicode: decodeURIComponent(options.q)
      })
      //赋值URL
      getApp().globalData.urlUnicode = decodeURIComponent(options.q)
      //打印二维码地址
      console.log("src=" + that.data.urlUnicode)
    } else {
      console.log('请您重新扫码,load')
      wx.showLoading({
        title: '请您重新扫码',
        // mask: true,
      })
      setTimeout(function () {

        wx.redirectTo({
          url: '/pages/index/index',
        })
      }, 2000)
      return
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    //加载数据
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    if (that.data.urlUnicode == '') { //判断URL地址
      that.data.urlUnicode = getApp().globalData.urlUnicode //url从新赋值
    }
    console.log("urlencode:" + that.data.urlUnicode)
    //打印机器号地址
    try {
      console.log(that.data.urlUnicode.match(/machno=(\S*)&version=1/)[1]);
    } catch (e) {
      console.log('请您重新扫码,show')
      wx.showLoading({
        title: '请您重新扫码',
      })
      setTimeout(function () {
        console.log('请您重新扫码,show')
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }, 2000)
      return
    }
    //验证地址
    //记录机器号码
    getApp().globalData.machineId = that.data.urlUnicode.match(/machno=(\S*)&version=1/)[1];
    //请求数据
    try {
      wx.request({
        url: getApp().globalData.requestUrl + '/getLotteryInfo',
        method: "post",
        data: {
          paramList: base64.CusBASE64.encoder('{"machineId":"' + getApp().globalData.machineId + '"}')
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res);
          if (res.statusCode == 200) {
            //16进制转String再转换成JSON数据
            if (res.data) {
              var dat = utils.stringToJson(utils.ascii2native(res.data))
              console.log('goucai:彩种信息')
              console.log(dat)
              //验证数据
              if (dat.status == 0) { //数据正确
                that.setData({ allLoading: true })
                // buyNavigateTo: that.data.buyNavigateTo + '?imageUrl=' + dat.dataList.picUrl + "&totalPrice=" + that.data.totalPrice
                that.setData({
                  imageUrltrue: dat.dataList.picUrl, //图片赋值
                  imageHidden: false,
                  standbyTicket: dat.dataList.standbyTicket, //机头余票赋值
                  price: dat.dataList.price, //机头彩种单价
                  buyUpperLimit: 10, //机头单词最高可购买数量
                  // buyUpperLimit: dat.dataList.buyUpperLimit, //机头单词最高可购买数量
                  totalPrice: dat.dataList.price * that.data.num, //当前订单总价
                  lotteryName: dat.dataList.lotteryName, //彩种名称
                  lotteryCode: dat.dataList.lotteryCode, //彩种编号
                })
                //验证彩种单价
                /*let re = /^[0-9]+$/
                if (!re.test(dat.dataList.price)) {
                  wx.showLoading({
                    title: '数据错误',
                    mask: true
                  })
                  return
                }*/
                if (parseInt(that.data.standbyTicket) <= 0) { //机头余量无票
                  wx.showToast({
                    title: '机头无票',
                    mask: true,
                    icon: 'none',
                    duration: 5000,
                  })
                }
                setTimeout(function () {
                  wx.hideLoading()
                }, 1000)
              } else { //数据错误，提示禁止购买
                wx.showLoading({
                  title: '数据错误',
                  mask: true
                })
                return
              }
            } else {
              wx.showLoading({
                title: '数据错误',
              })
              return
            }
          } else {
            // wx.hideLoading()
            wx.showLoading({
              title: '数据错误',
              mask: true
            })
            return
          }
        },
        fail: function (res) {
        }
      })
    } catch (res) {
      console.log(res)
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this
    that.setData({ allLoading: false })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  //减票
  jian: function () {
    let _self = this;
    if (this.data.num == 1) {
      return;
    } else {
      this.data.num = this.data.num - 1;
      this.setData({
        num: _self.data.num,
        totalPrice: _self.data.num * _self.data.price,
      })
    }
  },
  //加票
  jia: function () {
    let _self = this;
    if (this.data.num < this.data.standbyTicket && this.data.num < this.data.buyUpperLimit) {
      this.data.num = this.data.num + 1;
      this.setData({
        num: _self.data.num,
        totalPrice: _self.data.num * _self.data.price,
      })
    }
  },
  //全选
  selectall: function () {
    let _self = this;
    if (this.data.standbyTicket <= this.data.buyUpperLimit) {
      this.data.num = this.data.standbyTicket
    } else {
      this.data.num = this.data.buyUpperLimit
    }
    this.setData({
      num: _self.data.num,
      totalPrice: _self.data.num * _self.data.price,
    })
  },
  //下方切换隐藏
  navtap: function (res) {
    let type = res.currentTarget.dataset.type;
    this.data.type = type;
    this.setData({
      type: type
    })
  },
})